import React from 'react';
import {Row,Modal} from 'antd';
import qs from 'qs';
import BreadcrumbCustom from '../../../BreadcrumbCustom';
import * as Constant from "../../../../common/const";
import * as Request from '../../../../Server/Upgrade';
import {message} from "antd/lib/index";
import RegisterSettingCommonForm from "./SettingCommonForm";
import {AuthA,AuthButton} from '../../../auth/OperationAuth';
import TablePage from "../../../TablePage";

import {getPageParams} from '../../../../common/utils';
let defaultQuery = {page:Constant.DEFAULT_PAGE,size:Constant.DEFAULT_PAGE_SIZE};
const confirm = Modal.confirm;

class SettingCommon extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            dataSource:[],
            pagination:{},
            loading:false,
            title:undefined,
            visible:false,
            settingCommon:undefined
        };
    }

    componentDidMount() {
        this.fetchData();
    }

    componentWillUnmount() {
        this.setState = () => {
            return;
        };
    }


    fetchData = (params = defaultQuery) => {
        this.setState({
            loading:true
        });
        Request.settingCommonsPage(qs.stringify(params)).then(res => {
            if(Constant.REQUEST_SUCCESS == res.code){
                let data = res.data;
                this.setState({
                    dataSource:data.content,
                    pagination:{...this.state.pagination,total:data.totalSize,current:data.page + 1,pageSize:params.size},
                    loading:false,
                });
            }
        });
    }
    //新增
    add = ()=>{
        this.setState({
            title:'添加公共配置信息',
            visible:true,
            settingCommon:undefined
        })
    }

    //编辑
    edit = (id) => {
        Request.settingCommonGetById(id).then(res => {
            if(Constant.REQUEST_SUCCESS == res.code){
                this.setState({
                    visible: true,
                    settingCommon:res.data,
                    title:'修改公共配置信息'
                });
            }
        });
    }
    //删除
    delete = (id) => {
        confirm({
            title: '删除提示',
            content: '确认删除该条信息',
            maskClosable: true,
            okText: '确定',
            okType:'primary',
            cancelText: '取消',
            onOk:() => {
                Request.settingCommonDeleteById(qs.stringify({id:id})).then(res => {
                    if( Constant.REQUEST_SUCCESS == res.code){
                        this.fetchData(getPageParams(this.state.pagination,true));
                        message.success(Constant.DELETE_SUCCESS_TIP,Constant.AUTO_DISAPPEAR_TIME);
                    }
                })
            }
        });
    }
    cancel = (flag = false) => {
        if(typeof flag == 'object'){
            flag = false;
        }
        this.setState({
            visible:false
        });
        flag && this.fetchData(getPageParams(this.state.pagination));
    }
    /* 翻页 */
    handleTableChange = (pagination) => {
        const _this = this;
        const pager = { ..._this.state.pagination };
        pager.current = pagination.current;
        pager.pageSize = pagination.pageSize;
        _this.setState({
            pagination: pager,
            loading:true
        });
        _this.fetchData(getPageParams(pagination));
    }

    render(){

        const columns = [
            {
                title:"主键",
                dataIndex:"id",
                width:'16.66%',
                align:'center',
            },
            {
                title:"设置ID",
                dataIndex:"idMain",
                width:'16.66%',
                align:'center'
            },
            {
                title:"函数",
                dataIndex:"fun",
                width:'16.66%',
                align:'center'
            },{
                title:"参数",
                dataIndex:"param",
                width:'16.66%',
                align:'center'
            },
            {
                title:"备注",
                dataIndex:"remark",
                width:'16.66%',
                align:'center'
            },
            {
                title:"操作",
                width:'16.66%',
                align:'center',
                render:(text, record) =>
                    (
                        <span>
                             <AuthA onClick={this.edit.bind(this,record.id)} className={'operation-primary'} permission={'settingcomm:save'}>编辑</AuthA>
                             <AuthA onClick={this.delete.bind(this,record.id,record.name)} className={'operation-danger'} permission={'settingcomm:delete'}>删除</AuthA>
                         </span>
                    )
            }
        ];
        return (
            <div>
                <Row>
                    <BreadcrumbCustom first={'公共设置表'}/>
                </Row>
                <Row>
                    <AuthButton size={'large'} onClick={this.add} className={'ant-btn-large'} permission={'settingcomm:save'}>
                        添加信息
                    </AuthButton>
                </Row>
                <Row>
                    <TablePage
                        dataSource={this.state.dataSource}
                        columns={columns}
                        rowKey={record => record.id}
                        pagination={this.state.pagination}
                        loading={this.state.loading}
                        onChange={this.handleTableChange}
                    />
                </Row>
                <Row>
                    <Modal
                        title={this.state.title}
                        visible={this.state.visible}
                        footer={null}
                        onCancel={this.cancel}
                        maskClosable={false}
                        destroyOnClose={true}
                        centered={true}
                    >
                        <RegisterSettingCommonForm
                            cancel={this.cancel}
                            settingCommon={this.state.settingCommon}
                        />
                    </Modal>
                </Row>
            </div>
        );
    }
}

export default SettingCommon;